<template>
  <!--供方加扣款修改 -->
  <div class="e-form">
    <div class="tabs" v-if="Object.keys(basicFormData).length != 0">
      <el-tabs tab-position="left" v-model="tabsName" @tab-click="onChangeTab">
        <!-- 基本信息 -->
        <el-tab-pane
          label="基本信息"
          name="baseInfo"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="加扣款明细"
          name="AddDeductionsDetail"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="返还保证金"
          name="returnBail"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="附件信息"
          name="accessory"
          :disabled="clickTabFlag"
        ></el-tab-pane>
        <el-tab-pane
          label="审核历史"
          name="auditHistory"
          :disabled="clickTabFlag"
        ></el-tab-pane>

        <div>
          <!-- 基本信息 -->
          <div id="baseInfoCon" class="con">
            <div class="e-form">
              <div class="tabs">
                <div class="tabs-title" id="baseInfo">基本信息</div>
               <div style="width: 100%" class="form">
        <el-form :model="basicFormData" label-width="150px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="结算编号:">
                <el-input  v-model="basicFormData.billNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="单据机构:">
                <el-input
                  disabled
                  placeholder="请填写"
                  v-model="basicFormData.orgName"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="合同类型:">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.contractType"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合同名称:">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.contractName"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="结算单位:">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.settlementUnitName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="加扣款日期:">
                <el-date-picker
                  v-model="basicFormData.settlementDate"
                  type="date"
                  placeholder="选择日期"
                  :clearable="false"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="加扣款金额(元):">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.plusDeductionAmount"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="返还保证金金额(元)">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.returnDepositAmount"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="币种">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.currency"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="传财务共享">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.showEas"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="本位币:">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.baseCurName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态">
                {{ ['草稿','审核中','共享审核中','已审核','已关闭','已作废'][basicFormData.state] }}
                <!-- 草稿/审核中/共享审核中/已审核/已关闭/已作废 -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="加扣款金额(本位币):">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.baseCurPlusDeductionAmount"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="返还保证金金额:">
                <el-input
                  placeholder="请填写"
                  v-model="basicFormData.baseCurReturnDepositAmount"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="备注">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="basicFormData.remarks"
              >
              </el-input>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
              </div>
            </div>

            <!-- ------------------------------------------------------------------------------------- -->
          </div>

          <!-- 加扣款明细 -->
          <div id="AddDeductionsDetailCon" class="con">
            <div class="tabs-title" id="AddDeductionsDetail">加扣款明细</div>
            <div class="e-table">
              <div class="top">
                <div class="left">
                  <div>
                    <el-button
                      size="small"
                      style="background: rgb(75, 178, 255); color: #fff"
                      >总计划引入</el-button
                    >
                    <el-button
                      size="small"
                      style="background: #7aa818; color: #fff"
                      >删除</el-button
                    >
                  </div>
                </div>
              </div>
              <el-table
                :data="tableData"
                border
                style="width: 100%"
                @cell-mouse-enter="cellMouseEnter"
                @cell-mouse-leave="cellMouseLeave"
                :cell-class-name="cellClsNm"
                :header-cell-style="{ textAlign: 'center' }"
                :cell-style="{ textAlign: 'center' }"
              >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column prop="a1" label="类别" width="100">
                </el-table-column>
                <el-table-column prop="a" label="物资名称" width="100">
                </el-table-column>
                <el-table-column prop="b" label="规格型号" width="100">
                </el-table-column>
                <el-table-column prop="c" label="计量单位" width="100">
                </el-table-column>
                <el-table-column prop="d" label="材质" width="100">
                </el-table-column>
                <el-table-column prop="d" label="计划数量" width="100">
                </el-table-column>
                <el-table-column prop="d" label="设计数量" width="100">
                </el-table-column>
                <el-table-column prop="d" label="预算单价（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="预算金额（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="市场单价（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="市场金额（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="采购单位" width="100">
                </el-table-column>
                <el-table-column prop="d" label="采购方式" width="100">
                </el-table-column>
              </el-table>
              <!-- <div class="upload">
                                <el-upload class="upload-demo" drag action="https://jsonplaceholder.typicode.com/posts/" multiple>
                                    <el-button type="primary" style="padding: 20px 80px">上传附件</el-button>
                                    <div style="margin-top: 10px; font-size: 12px">鼠标拖入上传，按住Ctrl可多选附件</div>
                                </el-upload>
                            </div> -->
            </div>
          </div>

          <!-- 返还保证金 -->
          <div id="returnBailCon" class="con">
            <div class="tabs-title" id="returnBail">返还保证金</div>
            <div class="e-table">
              <div class="top">
                <div class="left">
                  <div>
                    <el-button
                      size="small"
                      style="background: rgb(75, 178, 255); color: #fff"
                      >总计划引入</el-button
                    >
                    <el-button
                      size="small"
                      style="background: #7aa818; color: #fff"
                      >删除</el-button
                    >
                  </div>
                </div>
              </div>
              <el-table
                :data="tableData"
                border
                style="width: 100%"
                @cell-mouse-enter="cellMouseEnter"
                @cell-mouse-leave="cellMouseLeave"
                :cell-class-name="cellClsNm"
                :header-cell-style="{ textAlign: 'center' }"
                :cell-style="{ textAlign: 'center' }"
              >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column prop="a1" label="类别" width="100">
                </el-table-column>
                <el-table-column prop="a" label="物资名称" width="100">
                </el-table-column>
                <el-table-column prop="b" label="规格型号" width="100">
                </el-table-column>
                <el-table-column prop="c" label="计量单位" width="100">
                </el-table-column>
                <el-table-column prop="d" label="材质" width="100">
                </el-table-column>
                <el-table-column prop="d" label="计划数量" width="100">
                </el-table-column>
                <el-table-column prop="d" label="设计数量" width="100">
                </el-table-column>
                <el-table-column prop="d" label="预算单价（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="预算金额（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="市场单价（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="市场金额（元）" width="130">
                </el-table-column>
                <el-table-column prop="d" label="采购单位" width="100">
                </el-table-column>
                <el-table-column prop="d" label="采购方式" width="100">
                </el-table-column>
              </el-table>
            </div>
          </div>
          <!-- 附件信息 -->
          <div id="accessoryCon" class="con">
            <div class="tabs-title" id="accessory">附件信息</div>
            <attach-comp
              :relationId="attachId"
              bucketName="materialsettle"
              v-if="objFlag.flag5"
            />
            <div class="seize-a-seat" v-else>
              <i class="el-icon-loading"></i>
            </div>
          </div>
          <!-- 审核历史 -->
          <div id="auditHistoryCon" class="con">
            <div class="tabs-title" id="auditHistory">审核历史</div>
            <div class="e-table">
              <el-table
                :data="tableData"
                border
                style="width: 100%"
                @cell-mouse-enter="cellMouseEnter"
                @cell-mouse-leave="cellMouseLeave"
                :cell-class-name="cellClsNm"
                :header-cell-style="{ textAlign: 'center' }"
                :cell-style="{ textAlign: 'center' }"
              >
                <el-table-column prop="a1" label="审核级次" width="200">
                </el-table-column>
                <el-table-column prop="a" label="审核人" width="200">
                </el-table-column>
                <el-table-column prop="b" label="审核人岗位" width="200">
                </el-table-column>
                <el-table-column prop="c" label="通知时间" width="200">
                </el-table-column>
                <el-table-column prop="c" label="审核时间" width="200">
                </el-table-column>
                <el-table-column prop="c" label="审核意见" width="200">
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </el-tabs>
    </div>
    <div id="seat" :style="{ height: seatHeight + 'px' }"></div>
    <div class="buttons">
      <el-button type="primary" style="background: #2e61d7">打印</el-button>
      <el-button type="primary" style="background: #2e61d7">冲红单</el-button>
      <el-button type="primary" style="background: #2e61d7">审核</el-button>
      <el-button type="primary" style="background: #2e61d7" @click="save()"
        >保存</el-button
      >
      <el-button type="primary" style="background: #2e61d7" @click="submit()"
        >保存并提交</el-button
      >

      <el-button type="primary" style="background: #2e61d7">撤回</el-button>
      <el-button type="danger" size="small" @click="ctDelete">删除</el-button>
      <el-button type="primary" style="background: #2e61d7">作废</el-button>
      <el-button @click="cancel()">关闭</el-button>
    </div>
    <span style="display: none">{{ localPopConfirm }}</span>
  </div>
</template>

<script>
import '@/utils/jquery.scrollTo.min'
import { mapMutations, mapState } from 'vuex'
import { showPromptPop, showSuccessPop } from '@/utils/common'
import AttachComp from '@/components/common/attachIndex'

export default {
    data () {
        return {
            tabsName: 'baseInfo',
            userName: '',
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: '',
            },
            tableData: [],
            value: '',
            textarea: '',
            formData: {},
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            //基本信息表单数据
            basicFormData: {
                billNo: '', //结算编号
                orgName: '', //单据机构
                contractType: '', //合同类型
                contractName: '', //合同名称
                settlementUnitName: '', //结算单位
                settlementDate: '', //加扣款日期
                plusDeductionAmount: '', //加扣款金额（元）
                returnDepositAmount: '', //返还保证金
                currency: '', //币种
                showEas: '', //传财务共享
                baseCurName: '', //本位币
                state: '', //状态
                baseCurPlusDeductionAmount: '', // /加扣款金额(本位币)
                baseCurReturnDepositAmount: '', //返还保证金额
                remarks: '', //备注
            },
            objFlag: {
                flag5: false,
            },
            billId: '',
            orgId: '', //机构id
            orgName: '', //机构名称
            isSkip: false, //是否跳转
        }
    },
    computed: {
        ...mapState({
            options: state => state.contract.ctClassify,
            popConfirm: 'popConfirm',
            popStatus: 'popStatus',
        }),
        localPopConfirm () {
            if (this.popConfirm) {
                switch (this.popStatus) {
                case 'prompt':
                    this.delData()
                    break
                case 'success':
                    if (this.isSkip) this.handleClose()
                    break
                }
                this.setPopConfirm(false)
            }
            return this.popConfirm
        },
        judgeRoute () {
            if (this.$route.path.includes('Add')) {
                return false
            }
            return true
        },
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        },
        attachId () {
            return this.$route.query.billid
        },
    },
    components: {
        AttachComp,
    },
    created () {
        let data = JSON.parse(localStorage.getItem('vuex'))
        this.orgId = data.userInfo.orgInfo.orgId
        this.orgName = data.userInfo.orgInfo.orgName
        this.billId = this.$route.query.billId
        this.getBasicData() //进入页面就获取基本信息数据
        this.getAttach()
    },
    mounted () {
    // 获取最后一个内容区域的高度，计算底部空白
        this.getLastConHeight()

        // 保存所有tabName
        const arr = [
            'baseInfo',
            'AddDeductionsDetail',
            'returnBail',
            'accessory',
            'auditHistory',
        ]
        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const $item = document.getElementById(item)
                    let itemTop = null
                    if ($item) {
                        itemTop = $item.offsetTop
                    }
                    return itemTop
                })
            }
            const scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if (scrollTop + 1 >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
        })

        this.screenWidth = document.documentElement.clientWidth
        this.screenHeight = document.documentElement.clientHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth
            this.screenHeight = document.documentElement.clientHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item).offsetTop
                return itemTop
            })
        })
    },
    methods: {
        ...mapMutations(['setPopConfirm']),

        //获取基本信息数据
        getBasicData () {
            this.$Ajax
                .httpGet({
                    url: '/materialsettle/supplierPlusDeductionSettlement/findById',
                    params: {
                        id: this.billId,
                    },
                })
                .then(res => {
                    this.basicFormData = res
                })
        },
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight () {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById('evaluateCon')) {
                    const lastConHeight =
            document.getElementById('evaluateCon').offsetHeight
                    console.log('lastConHeight: ', lastConHeight)
                    this.lastConHeight = lastConHeight
                    console.log('this.lastConHeight', this.lastConHeight)
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },

        //提交
        submit () {
            let settlementDate = this.getTime(this.basicFormData.settlementDate)
            this.$Ajax
                .httpPost({
                    url: '/materialsettle/supplierPlusDeductionSettlement/update',
                    params: {
                        baseCurId: '', //	本位币id
                        baseCurName: this.basicFormData.baseCurName, //本位币名称
                        baseCurPlusDeductionAmount: parseInt(this.basicFormData.baseCurPlusDeductionAmount), //	加扣款金额(本位币)
                        baseCurRate: 0, //本位币汇率
                        baseCurReturnDepositAmount: parseInt(this.basicFormData.baseCurReturnDepositAmount), //	返还保证金金额(本位币)
                        billId: this.billId, //	供方加扣款结算id
                        billNo: this.basicFormData.billNo, //	结算编号
                        contractBillId: '', //	合同id
                        contractBillNo: '', //	合同编号
                        contractName: this.basicFormData.contractName, //	合同名称
                        contractType: parseInt(this.basicFormData.contractType), //合同类型
                        dtls: [], //供方加扣款结算-加扣款明细集合
                        easId: '', //	财务Id
                        founderId: '', //	创建人Id
                        founderName: '', //创建人名称
                        gmtCreate: '', //	创建时间
                        gmtModified: '', //	更新时间
                        nullifyCreated: '', //	作废时间
                        nullifyCreator: '', //	作废人
                        nullifyCreatorId: '', //	作废人id
                        nullifyDescription: '', //	作废描述
                        nullifyReason: '', //	作废原因
                        orgId: this.orgId, //	所属机构id
                        orgName: this.basicFormData.orgName, //	所属机构名称
                        plusDeductionAmount: parseInt(this.basicFormData.plusDeductionAmount), //加扣款金额(元)
                        remarks: this.basicFormData.remarks, //备注信息
                        returnDepositAmount: parseInt(this.basicFormData.returnDepositAmount), //	返还保证金金额(元)
                        returnDepositDtls: [], //供方加扣款结算-返还保证金明细集合
                        rmbId: '', //	人民币id
                        rmbName: '', //	人民币名称
                        rmbPlusDeductionAmount: 0, //	加扣款金额(人民币)
                        rmbRate: 0, //人民币汇率
                        rmbReturnDepositAmount: 0, //	返还保证金金额(人民币)
                        settlementDate: settlementDate, //	结算日期
                        settlementUnitId: '', //	结算单位id
                        settlementUnitName: this.basicFormData.settlementUnitName, //	结算单位名称
                        state: this.basicFormData.state, //单据状态
                        workId: '', //流程id
                    },
                })
                .then(() => {
                    showSuccessPop('保存成功！')
                    this.isSkip = false
                    this.$router.push('/supplierAddDeductionsClose')
                })
        },
        //保存
        save () {},
        ctDelete () {
            showPromptPop('确定要删除吗？删除的数据可以在【回收站】还原或彻底删除。')
        },
        //删除
        delData () {
            this.$Ajax
                .httpGet({
                    url: '/materialsettle/supplierPlusDeductionSettlement/delete',
                    params: {
                        id: this.billId,
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                })
                .then(res => {
                    if (res.code == 200) {
                        showSuccessPop('删除成功！')
                        this.isSkip = true
                    }
                    this.$router.push('./supplierAddDeductionsClose')
                })
        },
        handleFullScreen () {
            this.fullscreen = !this.fullscreen
        },
        //查询附件信息
        getAttach () {
            this.objFlag.flag5 = true
        },
        //关闭
        cancel () {
            this.dialogVisible = false
            this.$router.push('./supplierAddDeductionsClose')
        },
        cellClsNm ({ column }) {
            if (column.label === '') {
                return 'fixed-left'
            }
        },
        cellMouseEnter (row) {
            this.MouseEnterId = row.billId
        },
        cellMouseLeave () {
            this.MouseEnterId = 0
        },
        onChangeTab (e) {
            console.log('clickTabFlag: ', this.clickTabFlag)
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
        },
        //日期转换格式
        getTime (dt) {
            var year = dt.getFullYear() //年
            var month = dt.getMonth() + 1 //月
            var date = dt.getDate() //日
            month = month < 10 ? '0' + month : month
            date = date < 10 ? '0' + date : date
            var str = year + '-' + month + '-' + date
            return str
        },
    },
}
</script>

<style lang="scss" scoped>
.buttons {
  left: 0;
}
.e-form {
  margin-bottom: 80px;
  padding: 0 20px;
  .tabs-title::before {
    content: "";
    height: 22px;
    width: 8px;
    border-radius: 40px;
    background-color: #2e61d7;
    display: block;
    position: absolute;
    left: 20px;
    margin-right: 20px;
  }
}

.e-table {
  background: #fff;
}
// ::v-deep.el-table__body-wrapper::-webkit-scrollbar{
//      width: 6px !important; // 横向滚动条
//     height: 10px !important// 纵向滚动条
// }

::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 6px; // 横向滚动条
  height: 10px; // 纵向滚动条
}
// 滚动条的滑块
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #ddd;
  border-radius: 5px;
}

.el-tab-pane {
  margin-left: -10px;
}
</style>